package com.zsc.externalservice.controller;


import com.zsc.externalservice.entity.external.CustomerDetails;
import com.zsc.externalservice.entity.external.CustomerInfo;
import com.zsc.externalservice.entity.external.CustomerInfo_2;
import com.zsc.externalservice.entity.external.SalesSummary;
import com.zsc.externalservice.entity.vo.CustomerVO;
import com.zsc.externalservice.services.impl.CustomerServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/api/customers")
//@Api(tags = "获取用户相关信息接口")
@Slf4j
public class CustomerController {

    @Resource
    private CustomerServiceImpl customerService;

//    curl -X POST "http://localhost:8082/api/customers/getByCustUuid" -H "Content-Type: application/json" -d "00000000000000000000442000100187"


    @PostMapping("/getByCustUuid")
    public CustomerInfo getCustomerByCustUuid(@RequestBody String custUuid) {
        if (custUuid.startsWith("\"") && custUuid.endsWith("\"")) {
            custUuid = custUuid.substring(1, custUuid.length() - 1);
        }
        return customerService.findCustomerByCustUuid(custUuid);
    }

    @PostMapping("/getByCustUuid_2")
    public CustomerInfo_2 getCustomerByCustUuid_2(@RequestBody String custUuid) {
        if (custUuid.startsWith("\"") && custUuid.endsWith("\"")) {
            custUuid = custUuid.substring(1, custUuid.length() - 1);
        }
        return customerService.findCustomerByCustUuid_2(custUuid);
    }

    @PostMapping("/getOrderByCustUuid")
    public List<SalesSummary> getOrderByCustUuid(@RequestBody String custUuid) {
        if (custUuid.startsWith("\"") && custUuid.endsWith("\"")) {
            custUuid = custUuid.substring(1, custUuid.length() - 1);
        }
        return customerService.getOrderByCustUuid(custUuid);
    }

    @PostMapping("/getCustomerDetailsCustUuid")
//    curl -X POST "http://localhost:8082/api/customers/getCustomerDetailsCustUuid" -H "Content-Type: application/json" -d "00000000000000000000442000100187"
    public CustomerDetails getCustomerDetailsCustUuid(@RequestBody String custUuid) {
        if (custUuid.startsWith("\"") && custUuid.endsWith("\"")) {
            custUuid = custUuid.substring(1, custUuid.length() - 1);
        }
        return customerService.getCustomerDetailsCustUuid(custUuid);
    }


    /**
     * 根据用户uuid获取用户信息
     *
     * @param custUuid 用户uuid
     * @return CustomerVO
     */
    @GetMapping("/getCustomInfo")
//    @ApiOperation("根据用户uuid获取用户信息")
    public CustomerVO getCustomerVO(String custUuid) {
        return customerService.getCustomerInfo(custUuid);
    }

    /**
     * 获取所用一般零售用户
     *
     * @return
     */
    @GetMapping("/getAllCustomer")
//    @ApiOperation("获取所用一般零售用户")
    public List<CustomerVO> getAllCustomer() {
        return customerService.getAllCustomer();
    }

    /**
     * 根据用户名获取到对应的用户uuid
     *
     * @param name
     * @return
     */
    @GetMapping("/getMangerUuid")
    public String getMangerUuid(@RequestHeader(value = "name") String name) {
        log.info("本次要查询的数据是(未解码)：{}", name);
        // 解码
        String decodedString;
        try {
            decodedString = URLDecoder.decode(name, String.valueOf(StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            log.error("解码失败，本次要解码的内容是：{}", name);
            return null;
        }
        return customerService.getManagerByName(decodedString);
    }

    /**
     * 根据许可证号获取客户信息
     * @param licenseCode
     * @return
     */
    @GetMapping("/getCustomerByLicenseCode")
    public CustomerVO getCustomerByLicenseCode(String licenseCode) {
        return customerService.getCustomerByLicenseCode(licenseCode);
    }
}
